package com.example.lotterysystem.common.interceptor;

import com.example.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        log.info("从header获取token:{}",token);
        log.info("获取路径:{}",request.getRequestURL());

        //校验token
        Claims claims = JWTUtil.parseJWT(token);
        if(null == claims) {
            //提示错误 设置状态码
            log.error("JWT令牌校验失败");
            response.setStatus(401);
            return false;
        }
        log.info("JWT令牌校验通过！");
        return true;

    }
}
